/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package music;

/**
 *
 * @author TJ
 */
import java.awt.Graphics;
import java.util.Iterator;
import music.global.CharacterMap;
import music.types.*;
import structures.NotesManager;
public class Measure {
    private Staff parent;;
    public ClefType type=null;
    public NotesManager notes1;
    private boolean renderTime=false,renderKey;
    private BarlineType barline=BarlineType.NORMAL;
    private NoteTypes clickDivision=NoteTypes.QUARTER;
    //public Measure() {init();}
    public Measure(Staff s2) {
        parent=s2;
        init();
    }
    public void init() {
        notes1=new NotesManager(this);
    }
    
    public void setBarline(BarlineType b) {barline=b;}
    public void setStaff(Staff t){parent=t;}
    public ClefType getClef() {
        if(type==null){return parent.getDefaultClefType();}
        return type;
    }
    public TimeSignature getTime(){
        return parent.getScore().getTimeAt(getMeasureNumber());}
    public int getMeasureNumber(){return parent.getMeasureNumber(this);}
    public NotesManager getNotes(){return notes1;}
    public int calculateWidth(SpacingOptions s,ClefType t,boolean renderTime){
        int x=0;
        Iterator i=notes1.getVoice(1).getIterator();
        double currentbeat=1;
        if(notes1.getVoice(1).isEmpty()) {
            //return;
        }
        ClefType lastClef=t;
        if(type!=null && t!=type) {
            lastClef=type;
            x+=type.getWidth(s);
        }
        else if(t==null && type==null) {        
            lastClef=parent.getDefaultClefType();
             x+=lastClef.getWidth(s);
        }
        TimeSignature timeAt=getTime();
        if(renderTime) {
            x+=timeAt.getTimeWidth(s);
        }
        while(i.hasNext()) {
            Note n=(Note)i.next();
            int w=n.calculateWidth(s);
            for(NoteTypes n2 : NoteTypes.getNotesWithin(timeAt, n.getBeat()-currentbeat)){
                x+=CharacterMap.getWidth(n2.getRest(), s.getRenderFont());
            }
            x+=w;
            currentbeat=n.getBeat()+n.getLength(timeAt);
            x+=15;
            
        }
        x+=barline.getWidth(s);
        return x;
    }
    public Staff getStaff(){return parent;}
    public ClefType render(Graphics g,SpacingOptions s, int x,ClefType t,boolean renderTime) {
        Iterator i=notes1.getVoice(1).getIterator();
        double currentbeat=1;
        if(notes1.getVoice(1).isEmpty()) {
            //return;
        }
        ClefType lastClef=t;
        if(type!=null && t!=type) {
            lastClef=type;
            type.render(g, parent.getBottomLine(), s, x);
            x+=type.getWidth(s);
        }
        else if(t==null && type==null) {        
            lastClef=parent.getDefaultClefType();
            lastClef.render(g, parent.getBottomLine(), s, x);
             x+=lastClef.getWidth(s);
        }
        TimeSignature timeAt=getTime();
        if(renderTime) {
            timeAt.renderTime(g, x, parent.getBottomLine(), s);
            x+=timeAt.getTimeWidth(s);
        }
        while(i.hasNext()) {
            Note n=(Note)i.next();
             int w=n.calculateWidth(s);
            for(NoteTypes n2 : NoteTypes.getNotesWithin(timeAt, n.getBeat()-currentbeat)){
                Rest r=new Rest(n2,this);
                r.renderNote(g,this.getStaff().getScore().getSpacing(),x, t);
                x+=CharacterMap.getWidth(n2.getRest(), s.getRenderFont());
            }
            n.renderNote(g,s, x, lastClef);
           x+=w;
            currentbeat=n.getBeat()+n.getLength(timeAt);
            x+=15;
            
        }
        x+=barline.renderBarline(g, s, x,parent.getBottomLine());
        return lastClef;
    }
}
